import output from "./output.json"
import {Counter__factory} from "./types/ethers";
import {BigNumber, ethers} from "ethers";

const provider = new ethers.providers.JsonRpcProvider("https://testnet.seutools.com/rpc")
// 如果是本地链，记得更换url
// const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545")
const contract = Counter__factory.connect(output.address, provider.getSigner())

const main = async () => {
  let res: { number: BigNumber; timestamp: BigNumber }

  res = await contract.show()
  console.log(`[时间:${res.timestamp}] 数值:${res.number}`);

  await contract.increase({gasPrice: 20000000000, gasLimit: 1e6})
  res = await contract.show()
  console.log(`[时间:${res.timestamp}] 数值:${res.number}`);

  await contract.decrease({gasPrice: 20000000000, gasLimit: 1e6})
  res = await contract.show()
  console.log(`[时间:${res.timestamp}] 数值:${res.number}`);

  console.log("---------增加记录---------")
  for (const event of await contract.queryFilter(contract.filters.CounterIncreaseEvent())) {
    console.log(`[时间:${event.args.timestamp}] 加1`);
  }

  console.log("---------减少记录---------")
  for (const event of await contract.queryFilter(contract.filters.CounterDecreaseEvent())) {
    console.log(`[时间:${event.args.timestamp}] 减1`);
  }
}

main().then()
